/**
 * @author LANG
 * Copyright 2009, QSC.
 * All rights reserved.
 */
float4x4 worldViewProjection : WorldViewProjection;

float4x4 lightViewProjection;
float4x4 viewProjInverse;

float3 lightPos;

sampler shadowMap;
sampler positionTex;
float power;
	  
struct VertexShaderInput {
	float4 position : POSITION;
	float2 tex : TEXCOORD0;  // Texture coordinates
};
 
VertexShaderInput vertexShaderFunction(VertexShaderInput input) {
	VertexShaderInput output;

	output.position = mul(input.position, worldViewProjection);
		
	output.tex = input.tex;
	return output;
}
  
float4 pixelShaderFunction(VertexShaderInput input): COLOR {
	input.tex.y = 1 - input.tex.y;
    
	float4 position = tex2D(positionTex, input.tex);
	
	float3 output = position.xyz;
	
	position.xy = position.xy * 2 - 1;
	position = mul(float4(position.xyz, 1), viewProjInverse);
	position.xyz /= position.w;
	
	float d = distance(position.xyz, lightPos);
	
	float4 posLightView = mul(float4(position.xyz, 1), lightViewProjection);
	posLightView.xyz /= posLightView.w;
	posLightView.x = 0.5 * posLightView.x + 0.5;
	posLightView.y = -0.5 * posLightView.y + 0.5;

	float shadow = tex2D(shadowMap, posLightView.xy).r + 10/(d*d - 2*d) > posLightView.z;
	
	return shadow;
}
  
// #o3d VertexShaderEntryPoint vertexShaderFunction
// #o3d PixelShaderEntryPoint pixelShaderFunction
// #o3d MatrixLoadOrder RowMajor